package com.lowan.iot.mqtt.sdk;

import com.lowan.iot.mqtt.sdk.init.InitExample;
import com.lowan.lib.mqtt.LowanMqttClient;
import lombok.extern.slf4j.Slf4j;

/**
 * @author y_sp
 * @date 2022 一月
 * <p>
 * mqtt 订阅 示例
 * 文档参照 @see https://doc.lowaniot.com/#/platform-access/MQTT-subscribe.html
 */
@Slf4j
public class SubscribeExample {

    /**
     * 订阅罗万智联平台设备信息
     * 格式：/lowaniot/租户编码/应用ID/产品编码
     */
    private static final String TOPIC = "/lowaniot/${tenandCode}/${appid}/${productCode}";

    public SubscribeExample() {
        // mqtt init
        InitExample.builder();
    }

    /**
     * 功能描述：订阅Mqtt信息
     */
    public void subscribe() {

        /** 开启线程监听*/
        new Thread(new Runnable() {
            @Override
            public void run() {
                new LowanMqttClient(TOPIC, (topic, payload) -> {
                    // onMessageArrived的实现
                    log.info(String.format("Rev Success ->: Topic: %s, Payload: %s", topic, new String(payload)));
                }).run();
            }
        }).start();

    }

    public static void main(String[] args) {
        // 订阅消息
        SubscribeExample subscribeInstance = new SubscribeExample();
        subscribeInstance.subscribe();
    }
}
